package com.ruoyi.resume.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.resume.domain.model.SchoolCategory;
import com.ruoyi.resume.domain.model.SplitResumeResult;
import com.ruoyi.resume.domain.model.resumeProfile.*;
import com.ruoyi.resume.exception.InvalidParameterException;
import com.ruoyi.resume.service.RadarChartService;
import com.ruoyi.resume.domain.model.ScoreTemplate;
import lombok.Cleanup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.time.YearMonth;
import java.time.temporal.ChronoUnit;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class RadarChartServiceImpl implements RadarChartService {

    @Autowired
    private ScoreTemplate scoreTemplate;

    private SchoolCategory schoolCategory;

    // 构造器加载学校类别信息
    public RadarChartServiceImpl() {
        try {
            // 加载学校类别信息
            ClassPathResource schoolResource = new ClassPathResource("schools.json");
            @Cleanup
            InputStream schoolInputStream = schoolResource.getInputStream();
            ObjectMapper objectMapper = new ObjectMapper();
            schoolCategory = objectMapper.readValue(schoolInputStream, SchoolCategory.class);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    @Override
    public Map<String, Integer> generateRadarChartDimensions(SplitResumeResult splitResumeResult) {
        Map<String, Integer> dimensions = new LinkedHashMap<>();

        // 获取 SplitResumeResult 中的基本信息和相关列表
        BasicInfo basicInfo = splitResumeResult.getBasicInfo();
        List<InternshipExperience> internshipExperiences = splitResumeResult.getInternshipExperiences();
        List<ProjectExperience> projectExperiences = splitResumeResult.getProjectExperiences();
        List<EducationBackground> educationBackgrounds = splitResumeResult.getEducationBackgrounds();
        List<WorkExperience> workExperiences = splitResumeResult.getWorkExperiences();

        // 1. 年龄评分
//        Map<String, Integer> ageScores = scoreTemplate.getScoreMap().get("年龄分值");
//        if (basicInfo.getAge() <= 25) {
//            dimensions.put("年龄", ageScores.get("25岁以下"));
//        } else if (basicInfo.getAge() <= 30) {
//            dimensions.put("年龄", ageScores.get("26-30岁"));
//        } else if (basicInfo.getAge() <= 35) {
//            dimensions.put("年龄", ageScores.get("31-35岁"));
//        } else {
//            dimensions.put("年龄", ageScores.get("36岁以上"));
//        }

        // 2. 学历评分 - 按照 basicInfo.getDegree 进行评分
        Map<String, Integer> educationScores = scoreTemplate.getScoreMap().get("学历分值");
        String degree = basicInfo.getDegree(); // 获取学历信息
        dimensions.put("学历", educationScores.getOrDefault(degree, 20)); // 根据学历评分，默认分值为 20

        // 3. 学校类别评分
        Map<String, Integer> schoolCategoryScores = scoreTemplate.getScoreMap().get("学校类别分值");
        String schoolClass = "本科及以下";
        for (String schoolName : educationScores.keySet()) {
            if (schoolName != null) {
                boolean is985 = schoolCategory.getIs985() != null && schoolCategory.getIs985().contains(schoolName);
                boolean is211 = schoolCategory.getIs211() != null && schoolCategory.getIs211().contains(schoolName);

                if (is985) {
                    schoolClass = "985";
                    break;
                } else if (is211) {
                    schoolClass = "211";
                }
            }
        }
        if ("本科及以下".equals(schoolClass) && basicInfo.getDegree().equals("本科")) {
            schoolClass = "本科";
        }
        dimensions.put("学校类别", schoolCategoryScores.getOrDefault(schoolClass, 60)); // 默认分值为 60

//        // 4. 实习经历评分
//        Map<String, Integer> internshipScores = scoreTemplate.getScoreMap().get("实习经历分值");
//        int internshipCount = internshipExperiences != null ? internshipExperiences.size() : 0;
//        if (internshipCount >= 3) {
//            dimensions.put("实习经历", internshipScores.get("3项及以上相关实习经历"));
//        } else if (internshipCount == 2) {
//            dimensions.put("实习经历", internshipScores.get("2项相关实习经历"));
//        } else if (internshipCount == 1) {
//            dimensions.put("实习经历", internshipScores.get("1项相关实习经历"));
//        } else {
//            dimensions.put("实习经历", internshipScores.get("无相关实习经历"));
//        }
//
//        // 5. 项目经历评分
//        Map<String, Integer> projectScores = scoreTemplate.getScoreMap().get("项目经历分值");
//        int projectCount = projectExperiences != null ? projectExperiences.size() : 0;
//        if (projectCount >= 3) {
//            dimensions.put("项目经历", projectScores.get("3项及以上优秀项目"));
//        } else if (projectCount == 2) {
//            dimensions.put("项目经历", projectScores.get("2项优秀项目"));
//        } else if (projectCount == 1) {
//            dimensions.put("项目经历", projectScores.get("1项优秀项目"));
//        } else {
//            dimensions.put("项目经历", projectScores.get("无项目经历"));
//        }

        // 4. 实践经历评分（合并实习和项目经历）
        Map<String, Integer> practiceScores = scoreTemplate.getScoreMap().get("实践经历分值");
        int internshipCount = internshipExperiences != null ? internshipExperiences.size() : 0;
        int projectCount = projectExperiences != null ? projectExperiences.size() : 0;
        int practiceCount = internshipCount + projectCount;

        if (practiceCount >= 3) {
            dimensions.put("实践经历", practiceScores.get("3项及以上相关实习或优秀项目经历"));
        } else if (practiceCount == 2) {
            dimensions.put("实践经历", practiceScores.get("2项相关实习或优秀项目经历"));
        } else if (practiceCount == 1) {
            dimensions.put("实践经历", practiceScores.get("1项相关实习或优秀项目经历"));
        } else {
            dimensions.put("实践经历", practiceScores.get("无相关实习或项目经历"));
        }


        // 6. 工作经历评分
        Map<String, Integer> workScores = scoreTemplate.getScoreMap().get("工作经历分值");

        YearMonth currentYearMonth = YearMonth.now();
        long totalMonths = 0;

        for (WorkExperience experience : workExperiences) {
            YearMonth startTime = experience.getStartTime();
            YearMonth endTime = experience.getEndTime() != null && experience.getEndTime().equals(YearMonth.of(9999, 1)) ?
                    currentYearMonth : experience.getEndTime();

            if (startTime == null || endTime == null) {
                throw new InvalidParameterException("工作经验起始时间或终止时间信息缺失");
            }

            totalMonths += ChronoUnit.MONTHS.between(startTime, endTime);
        }

        long totalYears = totalMonths / 12;

        if (totalYears >= 10) {
            dimensions.put("工作经历", workScores.get("10年以上相关工作经验"));
        } else if (totalYears >= 5) {
            dimensions.put("工作经历", workScores.get("5-10年相关工作经验"));
        } else if (totalYears >= 3) {
            dimensions.put("工作经历", workScores.get("3-5年相关工作经验"));
        } else if (totalYears >= 1) {
            dimensions.put("工作经历", workScores.get("少于3年相关工作经验"));
        } else {
            dimensions.put("工作经历", workScores.get("无相关工作经验"));
        }

        // 7. 职位评分
        Map<String, Integer> jobLevelScores = scoreTemplate.getScoreMap().get("职位层级分值");

        int highestJobScore = 30; // 默认无相关经验的最低分数
        String highestJobTitle = ""; // 用于记录最高分职位的名称

        // 遍历所有的实习经历，找到分数最高的职位
        for (WorkExperience experience : workExperiences) {
            String jobTitle = experience.getJobTitle(); // 获取职位信息
            int currentJobScore = getJobScore(jobTitle, jobLevelScores); // 根据职位获取分数

            if (currentJobScore > highestJobScore) {
                highestJobScore = currentJobScore;
                highestJobTitle = jobTitle;
            }
        }

        // 返回最终的职位评分
        dimensions.put("职位", highestJobScore);

        return dimensions;
    }

    // 计算职位评分的方法
    private int getJobScore(String jobTitle, Map<String, Integer> jobLevelScores) {
        if (jobTitle != null) {
            if (jobTitle.contains("CEO") || jobTitle.contains("CTO") || jobTitle.contains("CFO") || jobTitle.contains("总经理") || jobTitle.contains("执行董事") || jobTitle.contains("副总裁")) {
                return jobLevelScores.get("高层管理");
            } else if (jobTitle.contains("事业部总经理") || jobTitle.contains("分公司总经理") || jobTitle.contains("首席架构师") || jobTitle.contains("首席科学家")) {
                return jobLevelScores.get("中高层管理");
            } else if (jobTitle.contains("总监") || jobTitle.contains("部门负责人") || jobTitle.contains("技术主管") || jobTitle.contains("运营总监")) {
                return jobLevelScores.get("中层管理");
            } else if (jobTitle.contains("经理") || jobTitle.contains("团队负责人") || jobTitle.contains("资深产品经理") || jobTitle.contains("高级项目经理")) {
                return jobLevelScores.get("中基层管理");
            } else if (jobTitle.contains("高级架构师") || jobTitle.contains("技术专家") || jobTitle.contains("算法专家") || jobTitle.contains("产品专家")) {
                return jobLevelScores.get("核心技术/业务");
            } else if (jobTitle.contains("组长") || jobTitle.contains("项目负责人") || jobTitle.contains("带队工程师")) {
                return jobLevelScores.get("基层管理");
            } else if (jobTitle.contains("资深工程师") || jobTitle.contains("资深设计师") || jobTitle.contains("资深数据分析师")) {
                return jobLevelScores.get("资深员工");
            } else if (jobTitle.contains("工程师") || jobTitle.contains("运营专员") || jobTitle.contains("销售专员") || jobTitle.contains("数据分析师")) {
                return jobLevelScores.get("普通员工");
            } else if (jobTitle.contains("初级工程师") || jobTitle.contains("初级分析师") || jobTitle.contains("普通设计师")) {
                return jobLevelScores.get("初级员工");
            } else if (jobTitle.contains("实习生") || jobTitle.contains("助理") || jobTitle.contains("管培生") || jobTitle.contains("培训生")) {
                return jobLevelScores.get("助理/实习生");
            } else {
                return jobLevelScores.get("无相关经验");
            }
        }
        return jobLevelScores.get("无相关经验"); // 如果没有职位信息，默认无相关经验
    }

}
